﻿using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using XF.Common;

namespace KYBM
{
    public class DTOPrinter
    {
        private PrintPreviewDialog printPreview = new PrintPreviewDialog();
        private PrintDocument pdDocument = new PrintDocument();

        public XF.Model.Bill_DTOHead Order { get; set; }
        private int itemIndex = 0;
        public DTOPrinter(XF.Model.Bill_DTOHead order)
        {
            Order = order;

            //设置打印纸张和事件
            PaperSize pageSize = new PaperSize("DTOPaper", 800, 500);
            pdDocument.DefaultPageSettings.PaperSize = pageSize;
            pdDocument.BeginPrint += new PrintEventHandler(pdDocument_BeginPrint);
            pdDocument.PrintPage += new PrintPageEventHandler(pdDocument_OnPrintPage);
            pdDocument.EndPrint += new PrintEventHandler(pdDocument_EndPrint);
        }

        public void StartPrint()
        {
            //声明返回值的PageSettings A4/A5
            PageSettings ps = new PageSettings();
            //显示设置打印页对话框
            PageSetupDialog Psdl = new PageSetupDialog();
            //打印多页设置
            PrintDialog pt = new PrintDialog();
            pt.AllowCurrentPage = true;
            pt.AllowSomePages = true;
            pt.AllowPrintToFile = true;

            PrintPreviewDialog printPreviewDialog = new PrintPreviewDialog();

            Psdl.Document = pdDocument;
            printPreview.Document = pdDocument;
            printPreview.SetDesktopLocation(300, 800);
            Psdl.PageSettings = pdDocument.DefaultPageSettings;
            pt.Document = pdDocument;
            try
            {
                if (pt.ShowDialog() == DialogResult.OK)
                {
                    pdDocument.PrinterSettings.Copies = pt.PrinterSettings.Copies;
                }
                printPreview.ShowDialog();
                //调用打印
                //pdDocument.Print();

                // PrintDocument对象的Print()方法在PrintController类中执行PrintPage事件。
            }
            catch (InvalidPrinterException ex)
            {
                MessageBox.Show(ex.Message, "Simple Editor", MessageBoxButtons.OK, MessageBoxIcon.Error);
                throw;
            }
        }

        /// <summary>
        /// 每个打印任务只调用BeginPrint一次。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pdDocument_BeginPrint(object sender, PrintEventArgs e)
        {
            itemIndex = 0;
        }

        /// <summary>
        /// 绘制打印界面
        /// printDocument的PrintPage事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void pdDocument_OnPrintPage(object sender, PrintPageEventArgs e)
        {
            int heightTitle = 40;
            int heightSubTitle = 20;
            int heightContent = 30;
            int columnWidthTitle = 80;
            int logoSize = 80;
            Point pointStart = new Point(50, 20);
            Size sizeTable = new Size(e.PageBounds.Width - 100, e.PageBounds.Height - 100);

            Font fontTitle = new Font("宋体", 24, FontStyle.Bold);
            Font fontContent = new Font("宋体", 10);

            StringFormat sfTitle = new StringFormat();
            sfTitle.Trimming = StringTrimming.Word;
            sfTitle.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
            sfTitle.Alignment = StringAlignment.Center;
            sfTitle.LineAlignment = StringAlignment.Center;

            StringFormat sfContent = new StringFormat();
            sfContent.Trimming = StringTrimming.Word;
            sfContent.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
            sfContent.Alignment = StringAlignment.Near;
            sfContent.LineAlignment = StringAlignment.Center;

            StringFormat sfColumnTitle = new StringFormat();
            sfColumnTitle.Trimming = StringTrimming.Word;
            sfColumnTitle.FormatFlags = StringFormatFlags.NoWrap | StringFormatFlags.LineLimit | StringFormatFlags.NoClip;
            sfColumnTitle.Alignment = StringAlignment.Center;
            sfColumnTitle.LineAlignment = StringAlignment.Center;

            Point point = new Point(pointStart.X, pointStart.Y);

            //绘制表格
            Pen pen = new Pen(Color.Black);
            Rectangle r = new Rectangle(point, sizeTable);
            //e.Graphics.DrawRectangle(pen, r);//绘制外边框

            #region 绘制标题
            //logo
            point.X += 80;
            Size size = new Size(logoSize, logoSize);
            r = new Rectangle(point, size);
            e.Graphics.DrawImage(Image.FromFile(@".\Resources\logo.png"), r);
            //标题
            point.X += logoSize + 20;
            size = new Size(sizeTable.Width - point.X, heightTitle);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("瑞安市凯扬薄膜有限公司销售单", fontTitle, new SolidBrush(Color.Black), r, sfContent);
            //联系信息
            point.X += 5;
            point.Y += size.Height;
            size = new Size(sizeTable.Width - logoSize, heightSubTitle);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("地址：瑞安市塘下广场中路69号        塘下镇北街122号", fontContent, new SolidBrush(Color.Black), r, sfContent);

            //银行卡信息
            point.Y += size.Height;
            size = new Size(sizeTable.Width - logoSize, heightSubTitle);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("电话：13566150669/18072037511/15267783162  农行：62284 8033 17241 32211", fontContent, new SolidBrush(Color.Black), r, sfContent);

            #endregion
            #region  绘制抬头
            point.X = pointStart.X;
            //客户名称-标题
            point.Y += size.Height;
            size = new Size(columnWidthTitle, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("客户名称：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            //客户名称-值
            point.X += size.Width;
            size = new Size(sizeTable.Width - 220 - 120 * 2, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString(Order.CustomerName, fontContent, new SolidBrush(Color.Black), r, sfContent);
            //联系电话-标题
            point.X += size.Width;
            size = new Size(columnWidthTitle, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("联系电话：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            //联系电话-值
            point.X += size.Width;
            size = new Size(120, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString(Order.CustomerPhone, fontContent, new SolidBrush(Color.Black), r, sfContent);
            //制单人员-标题
            point.X += size.Width;
            size = new Size(columnWidthTitle, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("制单人员：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            //制单人员-值
            point.X += size.Width;
            size = new Size(columnWidthTitle, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString(Order.RealName, fontContent, new SolidBrush(Color.Black), r, sfContent);
            //客户地址-标题
            point.X = pointStart.X;
            point.Y += size.Height;
            size = new Size(columnWidthTitle, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("客户地址：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            //客户地址-值
            point.X += size.Width;
            size = new Size(sizeTable.Width - 20 - 120 * 2, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString(Order.CustomerAddress, fontContent, new SolidBrush(Color.Black), r, sfContent);
            ////联系人-标题
            //point.X += size.Width;
            //size = new Size(columnWidthTitle, heightContent);
            //r = new Rectangle(point, size);
            //e.Graphics.DrawString("联 系 人：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            ////联系人-值
            //point.X += size.Width;
            //size = new Size(120, heightContent);
            //r = new Rectangle(point, size);
            //e.Graphics.DrawString(string.Empty, fontContent, new SolidBrush(Color.Black), r, sfContent);
            //发货日期-标题
            point.X += size.Width;
            size = new Size(columnWidthTitle, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("发货日期：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            //发货日期-值
            point.X += size.Width;
            size = new Size(columnWidthTitle, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString(Order.CreateDate?.ToString("yyyy-MM-dd"), fontContent, new SolidBrush(Color.Black), r, sfContent);
            #endregion
            #region 绘制表格
            Point pointTableStart = new Point(pointStart.X, point.Y + size.Height);
            int rowCount = 10;
            point.X = pointTableStart.X;
            //绘制横线
            for (int i = 0; i < rowCount + 1; i++)
            {
                point.Y += size.Height;
                e.Graphics.DrawLine(pen, point.X, point.Y, point.X + sizeTable.Width, point.Y);
            }
            //绘制竖线
            point.X = pointTableStart.X;
            point.Y = pointTableStart.Y;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + rowCount * size.Height);//边缘线

            size = new Size(60, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("行号", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
            point.X += 60;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + (rowCount - 1) * size.Height);//行号

            size = new Size(200, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("规格型号", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
            point.X += 200;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + (rowCount - 1) * size.Height);//规格型号

            size = new Size(60, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("单位", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
            point.X += 60;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + (rowCount - 1) * size.Height);//单位

            size = new Size(80, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("数量", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
            point.X += 80;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + (rowCount - 1) * size.Height);//数量

            size = new Size(80, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("单价", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
            point.X += 80;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + (rowCount - 1) * size.Height);//单价

            size = new Size(100, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("金额", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
            point.X += 100;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + (rowCount - 1) * size.Height);//金额

            size = new Size(sizeTable.Width - 580, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("备注", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
            point.X = pointStart.X + sizeTable.Width;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + rowCount * size.Height);//备注边缘线

            decimal sum = 0;
            //绘制项目内容
            int endIndex = itemIndex + rowCount - 2 > Order.Items.Count ? Order.Items.Count : itemIndex + rowCount - 2;
            for (; itemIndex < endIndex; itemIndex++)
            {
                XF.Model.Bill_DTOItems item = Order.Items[itemIndex];
                point.X = pointTableStart.X;
                point.Y += heightContent;
                //行号
                size = new Size(60, heightContent);
                r = new Rectangle(point, size);
                e.Graphics.DrawString(zDataConverter.ToString(item.Item), fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                point.X += 60;
                //规格型号
                size = new Size(200, heightContent);
                r = new Rectangle(point, size);
                e.Graphics.DrawString(item.MaterielName + " " + item.MaterielModel, fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                point.X += 200;
                //单位
                size = new Size(60, heightContent);
                r = new Rectangle(point, size);
                switch(item.Unit)
                {
                    case 0:
                        e.Graphics.DrawString("只", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                        break;
                    case 1:
                        e.Graphics.DrawString("公斤", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                        break;
                    case 2:
                        e.Graphics.DrawString("件", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                        break;
                    case 3:
                        e.Graphics.DrawString("斤", fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                        break;
                    default:
                        break;
                }
                point.X += 60;
                //数量
                size = new Size(80, heightContent);
                r = new Rectangle(point, size);
                e.Graphics.DrawString(zDataConverter.ToDecimal(item.Qty).ToString("0.######"), fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                point.X += 80;
                //单价
                size = new Size(80, heightContent);
                r = new Rectangle(point, size);
                e.Graphics.DrawString(zDataConverter.ToDecimal(item.Price).ToString("0.######"), fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                point.X += 80;
                //金额
                size = new Size(100, heightContent);
                r = new Rectangle(point, size);
                e.Graphics.DrawString(zDataConverter.ToDecimal(item.Price * item.Qty).ToString("0.######"), fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                point.X += 100;
                //备注
                size = new Size(sizeTable.Width - 580, heightContent);
                r = new Rectangle(point, size);
                e.Graphics.DrawString(item.Remark, fontContent, new SolidBrush(Color.Black), r, sfColumnTitle);
                sum += (decimal)item.Price * (decimal)item.Qty;
            }


            //合计金额（大写）-标题
            point.X = pointTableStart.X;
            point.Y = pointTableStart.Y + (rowCount - 1) * size.Height;
            size = new Size(150, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("合计金额（大写）", fontContent, new SolidBrush(Color.Black), r, sfContent);
            point.X += size.Width;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + size.Height);//合计金额（大写）
            //合计金额（大写）-值
            size = new Size(330, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString(Rmb.CmycurD(sum), fontContent, new SolidBrush(Color.Black), r, sfContent);
            point.X += size.Width;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + size.Height);//合计金额（大写）
            //合计金额-标题
            size = new Size(100, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("合计金额", fontContent, new SolidBrush(Color.Black), r, sfContent);
            point.X += size.Width;
            e.Graphics.DrawLine(pen, point.X, point.Y, point.X, point.Y + size.Height);//合计金额
            size = new Size(sizeTable.Width - 580, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString(sum.ToString("0.#######"), fontContent, new SolidBrush(Color.Black), r, sfContent);
            #endregion
            #region 绘制页脚
            //银行卡信息
            point.X = pointStart.X;
            point.Y += size.Height;
            size = new Size(sizeTable.Width/2, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("送货单位及经手人（盖章）：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            //银行卡信息
            point.X += size.Width;
            size = new Size(sizeTable.Width/2, heightContent);
            r = new Rectangle(point, size);
            e.Graphics.DrawString("收货单位及经手人（盖章）：", fontContent, new SolidBrush(Color.Black), r, sfContent);
            #endregion
            if (itemIndex < Order.Items.Count)
            {
                e.HasMorePages = true;
            }
            else
            {
                //绘制完成后，关闭多页打印功能
                e.HasMorePages = false;
            }
        }


        /// <summary>  
        /// 每个打印任务只调用EndPrint一次。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void pdDocument_EndPrint(object sender, PrintEventArgs e)
        {
        }
    }
}
